<?php

use yii\db\Migration;

class m000007_000001_create_user_bill extends Migration
{
    public function safeUp()
    {
        $this->createTable('{{%user_bill}}', [
            'id' => $this->primaryKey(),
            'uid' => $this->integer()->notNull()->comment('用户编号'),
            'month' => $this->string(8)->notNull()->comment('月份Ym'),
            'package_id' => $this->integer()->notNull()->comment('套餐编号'),
            'price' => $this->decimal(12, 2)->notNull()->comment('单价'),
            'card_amount' => $this->integer()->notNull()->comment('计费卡数量'),
            'money' => $this->decimal(12, 2)->notNull()->comment('金额'),
            'status' => $this->integer()->notNull()->comment('状态'),
        ]);
        $this->createIndex('fk_user_bill_user1_idx', '{{%user_bill}}', ['uid']);
        try {
            $this->addForeignKey('fk_user_bill_user1', '{{%user_bill}}', ['uid'], '{{%user}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->createIndex('fk_user_bill_package1_idx', '{{%user_bill}}', ['package_id']);
        try {
            $this->addForeignKey('fk_user_bill_package1', '{{%user_bill}}', ['package_id'], '{{%package}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->batchInsert('{{%key_map}}', ['t', 'k', 'v'], [
            ['user_bill_status', 1, '空账单等待填充'],
            ['user_bill_status', 2, '已计算等待确认'],
            ['user_bill_status', 3, '已确认'],
        ]);
    }

    public function down()
    {
        $this->delete('{{%key_map}}', ['t' => 'user_bill_status']);
        $this->dropTable('{{%user_bill}}');
    }
}
